// pages/userinfo/userinfo.js
import validate from '../../utils/validate'
import toast from '../../utils/toast'
import {updateName} from "../../service/user";
import {updateUserInfoCache} from "../../utils/doLogin";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:{},
    error_name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      const {name} = wx.getStorageSync('userInfo')
    this.setData({
      name
    })
  },
  updateNickName(){
    //1.表单验证
    const check = this.checkInput()
    if(!check) return

      updateName({name:this.data.name}).then(()=>{
        updateUserInfoCache()
        toast.success('修改成功',()=>{
          wx.navigateBack()
        })
      })
  },
  /**
   * 验证用户输入的内容
   * @return {boolean}
   */
  checkInput(){
    const checkName = validate.min(this.data.name,2)

    //表单验证
    if(!checkName){
      this.setData({error_name: '昵称最少为两个字符'})
    }else{
      this.setData({error_name: ''})
    }
    if(checkName) return true
    return false
  }
})